var recChannelCtrl = myApp.controller('recChannelCtrl', ['$scope', '$rootScope', 'NgTableParams', function($scope, $rootScope, NgTableParams) {
	$rootScope.firstType = Tools.permissionList[0].permissionId;
	$rootScope.secondType = Tools.permissionList[0].sonPermission[8].permissionId;
		
	$scope.newChanItem = {};
	$scope.modChanItem = {};
	$scope.delChanItem = {};

	// 分页
	$scope.pageList = [];
	$scope.curPage = 1;
	$scope.totalPage = 1;
	$scope.totalRow = 0;
	var PAGESIZE = 10;

	// 提示弹层
	$scope.msg = "";
	$scope.visible = false;

	/**
	 * 获取用户信息列表
	 * @param  {[type]} curPage [当前页码]
	 */
	$scope.getChannelList = function(curPage) {
		var data = {
			curPage: curPage,
			pageSize: PAGESIZE
		}
		commonSrv.getChannelList(data, function(result) {
			var channelList = result.data.channelList;
            var channelTypeList = result.data.channelTypeList;
            var userList = result.data.userList;

            for(var i in channelList) {
                // 渠道分类
                channelList[i].channelTypeId = Tools.getItemById(channelList[i].channelTypeId, 'id', channelTypeList);
                // 拥有人
                channelList[i].owner = Tools.getItemById(channelList[i].owner, 'userId', userList);
            }
			$scope.channelList = new NgTableParams({count: PAGESIZE}, { counts: [],  dataset: channelList});
			
			// 获取分页信息
			var paginate = result.paginate;
			$scope.totalPage = paginate.totalPage;
			$scope.totalRow = paginate.totalRow;
			// 设置分页
			$scope.setPage(curPage);
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
	};
	// 设置分页
	$scope.setPage = function(curPage) {
		var newPageList = [];
		if (curPage < 1 || curPage > $scope.totalPage) {
			alert("设置分页失败");
			return;
		}
		var SHOWSIZE = 5;
		// 总页数小于SHOWSIZE
		if($scope.totalPage <= SHOWSIZE) {
			for(var i=0; i<$scope.totalPage; i++) {
				newPageList.push(i+1);
			}
		// 总页数大于等于SHOWSIZE
		} else {
			if(curPage > 2) {
				// 最后5页
				if(($scope.totalPage - curPage) < 2) {
					for(var i = ($scope.totalPage - 5); i < $scope.totalPage; i++) {
						newPageList.push(i + 1);
					}
				} else {
					for (var i = (curPage - 3); i < ((curPage + 2) > $scope.totalPage ? $scope.totalPage : (curPage + 2)); i++) {
                        newPageList.push(i + 1);
                    }
				}
			} else {
				for(var i=0; i<SHOWSIZE; i++) {
					newPageList.push(i + 1);
				}
			}
		}
        $scope.pageList = newPageList;
        $scope.curPage = curPage;
	};
	//设置当前选中页样式
    $scope.isActivePage = function (page) {
        return $scope.curPage == page;
    };
    //上一页
    $scope.previous = function () {
        if($scope.curPage > 1) {
        	$scope.getChannelList($scope.curPage - 1);
        }
    }
    //下一页
    $scope.next = function () {
        if($scope.curPage < $scope.totalPage) {
        	$scope.getChannelList($scope.curPage + 1);
        }
    };

    // 新增
    /**
     * 打开新增弹层
     * @return {[type]} [description]
     */
    $scope.openAdd = function() {
    	commonSrv.addChannelForm({}, function(result) {
    		if(result.status == 0) {
    			$scope.newChanItem = result.data;
    			$scope.newChanItem.name = "";
    			$scope.newChanItem.contact = "";
    			$scope.newChanItem.tel = "";
    			$scope.newChanItem.mobile = "";
    			$scope.newChanItem.email = "";
    			$scope.newChanItem.description = "";
    			$scope.newChanItem.select = {
    				channelTypeId: {},
    				owner: {}
    			};

                // warn
                $scope.newChanItem.warn = {
                    name: "",
                    contact: "",
                    tel: "",
                    mobile: "",
                    email: "",
                    description: "",
                    channelTypeId: "",
                    owner: ""
                };
    			$("#newChanModel").modal({backdrop: 'static', keyboard: false, show: true});
    		}
    	}, function(result) {
    		if (result.status == 1) {
    			alert(result.msg);
    		}
    	}, function() {
    		$scope.$apply();
    	});
    };
    /**
	 * 新增
	 */
    $scope.addChan = function() {
        $scope.watchName('newChanItem');
        $scope.watchChannelTypeId('newChanItem');
        $scope.watchOwner('newChanItem');

         // 验证
        if(!Tools.reg.isNull($scope.newChanItem.warn.name) || !Tools.reg.isNull($scope.newChanItem.warn.channelTypeId) || !Tools.reg.isNull($scope.newChanItem.warn.owner)) {
            alert("请修改表单");
            return;
        }

        // 名称
        var name = Tools.parseValue({target: $scope.newChanItem.name, targetValue: $scope.newChanItem.name, type: 'string'});      
        // 分类
        var channelTypeId = 0;
        if(!$scope.newChanItem.select.channelTypeId || $scope.newChanItem.select.channelTypeId == null) {
            channelTypeId = 0;
        } else {
            channelTypeId = Tools.parseValue({target: $scope.newChanItem.select.channelTypeId, targetValue: $scope.newChanItem.select.channelTypeId.id, type: 'number'});
        } 
        // 拥有人
        var owner = 0;
        if(!$scope.newChanItem.select.owner || $scope.newChanItem.select.owner == null) {
            owner = 0;
        } else {
            owner = Tools.parseValue({target: $scope.newChanItem.select.owner, targetValue: $scope.newChanItem.select.owner.userId, type: 'number'});
        }
        // 联系人
        var contact = Tools.parseValue({target: $scope.newChanItem.contact, targetValue: $scope.newChanItem.contact, type: 'string'});
        // 联系电话
        var tel = Tools.parseValue({target: $scope.newChanItem.tel, targetValue: $scope.newChanItem.tel, type: 'string'});
        // 联系手机
        var mobile = Tools.parseValue({target: $scope.newChanItem.mobile, targetValue: $scope.newChanItem.mobile, type: 'string'});
        // 联系邮件
        var email = Tools.parseValue({target: $scope.newChanItem.email, targetValue: $scope.newChanItem.email, type: 'string'});
        // 描述
        var description = Tools.parseValue({target: $scope.newChanItem.description, targetValue: $scope.newChanItem.description, type: 'string'});

    	var data = {
    		channelTypeId: channelTypeId,	
			contact: contact,	
			description: description,	
			email: email,	
			mobile: mobile,	
			name: name,	
			owner: owner,	
			tel: tel,
    	};
    	commonSrv.addChannel(data, function(result) {
    		if(result.status == 0) {
    			$scope.msg = "新增成功";
				$scope.visible = true;
				$scope.getChannelList($scope.curPage);
				$scope.newChanItem = {};
    			$("#newChanModel").modal('hide');
    		}
    	}, function(result) {
    		if (result.status == 1) {
    			alert(result.msg);
    		}
    	}, function() {
    		$scope.$apply();
    	});
    };
    // 删除
	/**
	 * 打开删除面板
	 * @param  {[type]} item [description]
	 * @return {[type]}      [description]
	 */
	$scope.openDel = function(item) {
		$scope.delChanItem = item;
		$('#delChanModel').modal('show');
	};
	/**
	 * 删除渠道
	 * @param  {[type]} item [description]
	 * @return {[type]}      [description]
	 */
	$scope.delChan = function(item) {
		var data = {
			channelId: $scope.delChanItem.channelId
		};
		commonSrv.delChannel(data, function(result) {
			if(result.status == 0) {
				$scope.msg = "删除成功";
				$scope.visible = true;
				$("#delChanModel").modal('hide');
				$scope.delChanItem = {};
                $scope.getChannelList($scope.curPage);
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
			$("#delChanModel").modal('hide');
		}, function() {
			$scope.$apply();
		});
	};
    
	// 修改
	/**
     * 打开修改弹层
     * @return {[type]} [description]
     */
    $scope.openEdit = function(item) {
    	var data = {channelId: item.channelId};
    	commonSrv.editChannelForm(data, function(result) {
    		if(result.status == 0) {
    			$scope.modChanItem = result.data;
                $scope.modChanItem.select = {
                    channelTypeId: Tools.getItemById($scope.modChanItem.channel.channelTypeId, 'id', $scope.modChanItem.channelTypeList),
                    owner: Tools.getItemById($scope.modChanItem.channel.owner, 'userId', $scope.modChanItem.userList)
                };

                // warn
                $scope.modChanItem.warn = {
                    name: "",
                    contact: "",
                    tel: "",
                    mobile: "",
                    email: "",
                    description: "",
                    channelTypeId: "",
                    owner: ""
                };
    			$("#editChanModel").modal({backdrop: 'static', keyboard: false, show: true});
    		}
    	}, function(result) {
    		if (result.status == 1) {
    			alert(result.msg);
    		}
    	}, function() {
    		$scope.$apply();
    	})
    };
    /**
	 * 修改
	 */
    $scope.editChan = function() {
        $scope.watchName('modChanItem');
        $scope.watchChannelTypeId('modChanItem');
        $scope.watchOwner('modChanItem');

        // 验证
        if(!Tools.reg.isNull($scope.modChanItem.warn.name) || !Tools.reg.isNull($scope.modChanItem.warn.channelTypeId) || !Tools.reg.isNull($scope.modChanItem.warn.owner)) {
            alert("请修改表单");
            return;
        }

        // 名称
        var name = Tools.parseValue({target: $scope.modChanItem.channel.name, targetValue: $scope.modChanItem.channel.name, type: 'string'});      
        // 分类
        var channelTypeId = 0;
        if(!$scope.modChanItem.select.channelTypeId || $scope.modChanItem.select.channelTypeId == null) {
            channelTypeId = 0;
        } else {
            channelTypeId = Tools.parseValue({target: $scope.modChanItem.select.channelTypeId, targetValue: $scope.modChanItem.select.channelTypeId.id, type: 'number'});
        } 
        // 拥有人
        var owner = 0;
        if(!$scope.modChanItem.select.owner || $scope.modChanItem.select.owner == null) {
            owner = 0;
        } else {
            owner = Tools.parseValue({target: $scope.modChanItem.select.owner, targetValue: $scope.modChanItem.select.owner.userId, type: 'number'});
        }
        // 联系人
        var contact = Tools.parseValue({target: $scope.modChanItem.channel.contact, targetValue: $scope.modChanItem.channel.contact, type: 'string'});
        // 联系电话
        var tel = Tools.parseValue({target: $scope.modChanItem.channel.tel, targetValue: $scope.modChanItem.channel.tel, type: 'string'});
        // 联系手机
        var mobile = Tools.parseValue({target: $scope.modChanItem.channel.mobile, targetValue: $scope.modChanItem.channel.mobile, type: 'string'});
        // 联系邮件
        var email = Tools.parseValue({target: $scope.modChanItem.channel.email, targetValue: $scope.modChanItem.channel.email, type: 'string'});
        // 描述
        var description = Tools.parseValue({target: $scope.modChanItem.channel.description, targetValue: $scope.modChanItem.channel.description, type: 'string'});

        var data = {
            name: name,
            channelTypeId: channelTypeId,
            owner: owner,
            contact: contact,
            tel: tel,
            mobile: mobile,
            email: email,
            description: description,
            channelId: $scope.modChanItem.channel.channelId
        };
        commonSrv.editChannel(data, function(result) {
            if(result.status == 0) {
                $scope.msg = "修改成功";
                $scope.visible = true;
                $scope.getChannelList($scope.curPage);
                $('#editChanModel').modal('hide');
            }
        }, function(result) {
            if (result.status == 1) {
                alert(result.msg);
            }
        }, function() {
            $scope.$apply();
        })
    };

    // 验证
    // 名称
    $scope.watchName = function(type) {
        var nullWarn = "名称不能为空";
        if(type == 'modChanItem') {
            if(Tools.reg.isNull($scope[type].channel.name)) {
                $scope[type].warn.name = nullWarn;
            } else {
                $scope[type].warn.name = "";
            }
        } else {
            if(Tools.reg.isNull($scope[type].name)) {
                $scope[type].warn.name = nullWarn;
            } else {
                $scope[type].warn.name = "";
            }
        }
    };
    // 分类
    $scope.watchChannelTypeId = function(type) {
        var nullWarn = "请选择类型";
        if($scope[type].select.channelTypeId == null || !$scope[type].select.channelTypeId.id) {
            $scope[type].warn.channelTypeId = nullWarn;
        } else {
            $scope[type].warn.channelTypeId = "";
        }
    };
    // 拥有人
    $scope.watchOwner = function(type) {
        var nullWarn = "请选择类型";
        if($scope[type].select.owner == null || !$scope[type].select.owner.userId) {
            $scope[type].warn.owner = nullWarn;
        } else {
            $scope[type].warn.owner = "";
        }
    };

	// 初始化
	function init() {
		$scope.getChannelList(1);
		$(document).ready(function() {
			// console.log($rootScope.secondType);
		});
	}
	init();
}]);